package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.commom.exception.CustException;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.dtos.UserRelationDTO;
import com.heima.model.user.pojo.ApUser;
import com.heima.user.service.ApUserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

/**
 * @author by jojo
 * @Date 2022/3/7
 * @Description
 */
@Service
@Slf4j
public class ApUserRelationServiceImpl implements ApUserRelationService {
   @Autowired
   private StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult follow(UserRelationDTO dto) {
        //参数校验
        //检验用户是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser==null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto.getAuthorApUserId()==null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"作者关联的userId为空");
        }
        //判断操作方式 0 关注 1取关
        Short operation = dto.getOperation();
        if (operation==null||(!UserRelationConstants.OPERATION_ATTENTION.equals(operation)&&!UserRelationConstants.OPERATION_ATTENTION_TAKE_OF.equals(operation))) {
            log.error("操作参数为null或操作方式有误!!!");
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"操作参数为null或操作方式有误。。。");
        }
        //判断登录用户id和作者对应的用户id
        Integer loginUserId = apUser.getId();
        Integer followUserId = dto.getAuthorApUserId();
        if (loginUserId.equals(followUserId)) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"自己无法关注自己！！！");
        }
        //校验之前是否关注过该作者
        ZSetOperations<String, String> zSet = redisTemplate.opsForZSet();
        // 参数1： key   参数2: 要查询集合元素
        Double score = zSet.score(UserRelationConstants.FOLLOW_LIST + loginUserId, String.valueOf(followUserId));
        if (operation.equals(UserRelationConstants.OPERATION_ATTENTION)&&score!=null) {
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"您已经关注该作者！！！");
        }
        // 判断操作方式是关注（0），还是取关（1）
        try {
            if (UserRelationConstants.OPERATION_ATTENTION.equals(operation)) {
                //添加关注关系
                zSet.add(UserRelationConstants.FOLLOW_LIST+loginUserId,String.valueOf(followUserId),System.currentTimeMillis());
                //添加作者的粉丝
                zSet.add(UserRelationConstants.FANS_LIST+followUserId,String.valueOf(loginUserId),System.currentTimeMillis());
            }else {
                //取关
                //删除关注关系
                zSet.remove(UserRelationConstants.FOLLOW_LIST+loginUserId,String.valueOf(followUserId));
                //删除粉丝关系
                zSet.remove(UserRelationConstants.FANS_LIST+followUserId,String.valueOf(loginUserId));
            }
        } catch (Exception e) {
            e.printStackTrace();
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,"关注或取关操作失败");
        }
        return ResponseResult.okResult();
    }
}
